<?php

/**

 * indec cat Widget

 * @package WordPress

 * @subpackage wheatv

 */



class wheatv_indexcat_articles extends WP_Widget {

   function wheatv_indexcat_articles() {

		$widget_ops = array('description' => '在首页显示特定分类下的文章列表，只能用于首页' );

		parent::WP_Widget(false, __('骑友公社: 首页分类栏目', 'wheatv'),$widget_ops);      

	}

   function widget($args, $instance) {
        extract( $args );
		$num = $instance['num'];
		$title = apply_filters('widget_title', $instance['title'] );
		$category= wheatv_get_cat_slug($instance['category']);
		
		echo $before_widget;
		echo $before_title; 
		_e($title,'wheatv');
		echo $after_title;
		?>
            <?php
			query_posts("showposts=1&category_name=".$category);
			while(have_posts()) : the_post(); ?>
            <div class="in_same_1">
             <?php if(has_post_thumbnail()):?>
              <a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><?php the_post_thumbnail('catnews');?></a>
             <?php endif;?>
              <h2><a href="<?php the_permalink(); ?>" target="_blank"><?php echo wheatv_excerpt(get_the_title(),18);?></a></h2>
              <p><?php echo wheatv_excerpt(strip_tags(get_the_content()),70);?></p>
            </div>
            <?php endwhile; 
				 wp_reset_query();
			?>
            <div class="in_same_2">
              <ul>
              <?php
			  query_posts("showposts=".$num."&offset=-1&category_name=".$category);
			  while (have_posts()) : the_post(); ?>
                <li><a href="<?php the_permalink(); ?>"><?php echo wheatv_excerpt(get_the_title(),22);?></a></li>
              <?php endwhile;
			  wp_reset_query();
	          ?>
              </ul>
            </div>
        <?php
		echo $after_widget;
   }
   
   function update($new_instance, $old_instance) {
	   $instance['category'] = strip_tags( $new_instance['category'] );
	   $instance['num'] = strip_tags($new_instance['num']);     
	   $instance['title'] = strip_tags( $new_instance['title'] );           
       return $instance;
   }
   function form($instance) {  
	   $defaults = array( 'title' => __('栏目未命名', '栏目未命名'),'num' => '5');
	   $instance = wp_parse_args( (array) $instance, $defaults ); 

	   $title = esc_attr($instance['title']);
	   $category = esc_attr($instance['category']);
	   $num = esc_attr($instance['num']);
   ?>
    <p>
		<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('栏目标题:', 'wheatv'); ?></label>
		<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $title; ?>" />
	</p>
    <p>
        <label for="<?php echo $this->get_field_id('num'); ?>"><?php _e('列表文章数:','wheatv'); ?></label>
        <input type="text" name="<?php echo $this->get_field_name('num'); ?>" value="<?php echo $num; ?>" id="<?php echo $this->get_field_id('num'); ?>" />
    </p>
    <p>
            <label for="<?php echo $this->get_field_id('category'); ?>"><?php _e('所属分类:','wheatv'); ?></label>
            <select name="<?php echo $this->get_field_name('category'); ?>" class="widefat" id="<?php echo $this->get_field_id('category'); ?>">
                <?php 
				$thecatlist_A = get_categories('hide_empty=0');
                $catdlist = array();
				foreach ($thecatlist_A as $catforlist){ 
				$cate_name = $catforlist->cat_name;
				?>
                <option value="<?php echo $cate_name;?>" <?php if($category == $cate_name){ echo "selected='selected'";} ?>><?php echo $cate_name; ?></option>   
                <?php }?>       
            </select>
     </p>
   <?php
   }
}
register_widget('wheatv_indexcat_articles');
?>